package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.queryObject.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyComment strategyComment, UserInfo userInfo) {

        // 利用bean工具类把用户对象中的数据复制到评论对象中
        // 低层是使用内省实现的
        BeanUtils.copyProperties(userInfo, strategyComment);

        // 设置创建时间
        strategyComment.setCreateTime(new Date());

        strategyComment.setUserId(userInfo.getId());

        // 以防万一把要保存的对象的id设置为空
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        Optional<StrategyComment> strategyComment = strategyCommentRepository.findById(id);
        return strategyComment.orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        List<StrategyComment> all = strategyCommentRepository.findAll();
        return all;
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQueryObject qo) {

        // 封装查询条件
        Query query = new Query();

        // 根据攻略id作为条件查评论
        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }

        // 总条数
        long totalCount = template.count(query, StrategyComment.class);

        // 如果总条数为0 , 那么返回空的分页对象
        if (totalCount==0){
            return Page.empty();
        }

        // 分页对象 , 参数1为当前页 从0开始 , 参数2为每页显示条数 , 参数3为排序方式, 参数4为按哪个字段排序
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(),
                Sort.Direction.ASC,"id");

        query.with(pageable);// 添加查询条件

        List<StrategyComment> list = template.find(query, StrategyComment.class);

        // PageImpl 为 Page 接口的一个实现类
        return new PageImpl<>(list, pageable,totalCount );
    }

    @Override
    public void commentThumb(String cid, UserInfo userInfo) {

        // 被点赞的评论
        StrategyComment strategyComment = strategyCommentRepository.findById(cid).orElse(null);
        // 点赞过该评论的用户的列表
        List<String> thumbuplist = strategyComment.getThumbuplist();
        // 如果已经在该列表中表示进行的是取消点赞操作
        if (thumbuplist.contains(userInfo.getId())) {
            // 那么删除在已点赞用户列表中当前用户的id
            thumbuplist.remove(userInfo.getId());
            // 并且该评论的点赞数 - 1
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
        }else { // 不存在代表的是点赞操作
            // 那么添加当前登录用户的id到列表中
            thumbuplist.add(userInfo.getId());
            // 并且该评论的点赞数 + 1
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
        }

        // 修改后保存
        strategyCommentRepository.save(strategyComment);
    }
}
